/*
 * Ethan Levine
 * 
 * The contents of this file are subject to the GNU General Public
 * License (GPL) v2.  The full contents of this license can be
 * found at Mathma's website, http://www.codeplex.com/mathma/license
 */

package mathma.core.simp;

import mathma.core.expr.Expression;

import mathma.core.value.Value;

import java.util.HashMap;
import java.util.Map;

/**
 * @since 0.1.6
 * @author Ethan Levine
 */
public abstract class TargettedSimplificationAlgorithm extends SimplificationAlgorithm {

    private Map<Class, Fragment> frags;
    
    protected TargettedSimplificationAlgorithm() {
        frags = new HashMap<Class, Fragment>();
    }
    
    @Override
    protected <T extends Value> Expression<T> simplifySingle(Expression<T> expr) {
        if (frags.containsKey(expr.getClass())) {
            return frags.get(expr.getClass()).simp(expr);
        } else {
            return expr;
        }
    }
    
    protected void mapFragment(Class fragClass, Fragment frag) {
        frags.put(fragClass, frag);
    }
    
    protected interface Fragment<E extends Value> {
        Expression<E> simp(Expression<E> expr);
    }
}
